﻿namespace HRM.CORE.Option
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using APP.HELPER;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms; using Common;
    using Common;

    public class xucHeaderReportOption : Common.xucBase
    {
        private SimpleButton btFontCompanyAddress;
        private SimpleButton btFontCompanyName;
        private CalcEdit calImageHeight;
        private CalcEdit calImageWidth;
        private CheckEdit cheIsShowLogo;
        private CheckEdit cheIsUperCompanyName;
        private IContainer components = null;
        private FontDialog fontDialog1;
        private GroupBox gbDefault;
        private GroupBox gbImage;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private LabelControl labelControl5;
        private LabelControl labelControl6;
        private PictureEdit ptImageHeader;
        private RadioButton rdStyleDefault;
        private RadioButton rdStyleImage;
        private TextEdit txtFontCompanyAddress;
        private TextEdit txtFontCompanyName;
        private string xmlFormatOptionPath = (Application.StartupPath + @"\Layout\headerReportFormatOption.xml");

        public xucHeaderReportOption()
        {
            this.InitializeComponent();
        }

        private void btFontCompanyAddress_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontCompanyAddress.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontCompanyAddress.Font = this.fontDialog1.Font;
                this.txtFontCompanyAddress.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void btFontCompanyName_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontCompanyName.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontCompanyName.Font = this.fontDialog1.Font;
                this.txtFontCompanyName.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.fontDialog1 = new FontDialog();
            this.gbImage = new GroupBox();
            this.labelControl5 = new LabelControl();
            this.labelControl2 = new LabelControl();
            this.labelControl6 = new LabelControl();
            this.calImageHeight = new CalcEdit();
            this.labelControl1 = new LabelControl();
            this.calImageWidth = new CalcEdit();
            this.ptImageHeader = new PictureEdit();
            this.gbDefault = new GroupBox();
            this.cheIsUperCompanyName = new CheckEdit();
            this.cheIsShowLogo = new CheckEdit();
            this.txtFontCompanyAddress = new TextEdit();
            this.btFontCompanyName = new SimpleButton();
            this.txtFontCompanyName = new TextEdit();
            this.labelControl4 = new LabelControl();
            this.btFontCompanyAddress = new SimpleButton();
            this.labelControl3 = new LabelControl();
            this.rdStyleDefault = new RadioButton();
            this.rdStyleImage = new RadioButton();
            this.gbImage.SuspendLayout();
            this.calImageHeight.Properties.BeginInit();
            this.calImageWidth.Properties.BeginInit();
            this.ptImageHeader.Properties.BeginInit();
            this.gbDefault.SuspendLayout();
            this.cheIsUperCompanyName.Properties.BeginInit();
            this.cheIsShowLogo.Properties.BeginInit();
            this.txtFontCompanyAddress.Properties.BeginInit();
            this.txtFontCompanyName.Properties.BeginInit();
            base.SuspendLayout();
            this.gbImage.Controls.Add(this.labelControl5);
            this.gbImage.Controls.Add(this.labelControl2);
            this.gbImage.Controls.Add(this.labelControl6);
            this.gbImage.Controls.Add(this.calImageHeight);
            this.gbImage.Controls.Add(this.labelControl1);
            this.gbImage.Controls.Add(this.calImageWidth);
            this.gbImage.Controls.Add(this.ptImageHeader);
            this.gbImage.Location = new Point(13, 0xc3);
            this.gbImage.Name = "gbImage";
            this.gbImage.Size = new Size(0x1d9, 0x92);
            this.gbImage.TabIndex = 2;
            this.gbImage.TabStop = false;
            this.gbImage.Text = "Chọn h\x00ecnh ảnh";
            this.labelControl5.Location = new Point(0x138, 0x74);
            this.labelControl5.Name = "labelControl5";
            this.labelControl5.Size = new Size(6, 13);
            this.labelControl5.TabIndex = 20;
            this.labelControl5.Text = "x";
            this.labelControl2.AllowHtmlString = true;
            this.labelControl2.Location = new Point(0x18b, 0x74);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new Size(20, 14);
            this.labelControl2.TabIndex = 0x13;
            this.labelControl2.Text = "<color=blue>(px)</color>";
            this.labelControl6.AllowHtmlString = true;
            this.labelControl6.Location = new Point(0x11e, 0x74);
            this.labelControl6.Name = "labelControl6";
            this.labelControl6.Size = new Size(20, 14);
            this.labelControl6.TabIndex = 0x12;
            this.labelControl6.Text = "<color=blue>(px)</color>";
            this.calImageHeight.Location = new Point(0x144, 0x71);
            this.calImageHeight.Name = "calImageHeight";
            this.calImageHeight.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.calImageHeight.Size = new Size(0x41, 20);
            this.calImageHeight.TabIndex = 0x11;
            this.labelControl1.Location = new Point(14, 0x74);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new Size(0xc1, 13);
            this.labelControl1.TabIndex = 0x10;
            this.labelControl1.Text = "K\x00edch cỡ thay đổi (chiều rộng x chiều cao)";
            this.calImageWidth.Location = new Point(0xd7, 0x71);
            this.calImageWidth.Name = "calImageWidth";
            this.calImageWidth.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.calImageWidth.Size = new Size(0x41, 20);
            this.calImageWidth.TabIndex = 1;
            this.ptImageHeader.Dock = DockStyle.Top;
            this.ptImageHeader.Location = new Point(3, 0x11);
            this.ptImageHeader.Name = "ptImageHeader";
            this.ptImageHeader.Properties.NullText = "[Nhấp chuột phải v\x00e0o đ\x00e2y, chọn Load để mở tập tin h\x00ecnh ảnh]";
            this.ptImageHeader.Properties.SizeMode = PictureSizeMode.Stretch;
            this.ptImageHeader.Size = new Size(0x1d3, 0x55);
            this.ptImageHeader.TabIndex = 0;
            this.ptImageHeader.ImageChanged += new EventHandler(this.ptImageHeader_ImageChanged);
            this.gbDefault.Controls.Add(this.cheIsUperCompanyName);
            this.gbDefault.Controls.Add(this.cheIsShowLogo);
            this.gbDefault.Controls.Add(this.txtFontCompanyAddress);
            this.gbDefault.Controls.Add(this.btFontCompanyName);
            this.gbDefault.Controls.Add(this.txtFontCompanyName);
            this.gbDefault.Controls.Add(this.labelControl4);
            this.gbDefault.Controls.Add(this.btFontCompanyAddress);
            this.gbDefault.Controls.Add(this.labelControl3);
            this.gbDefault.Location = new Point(13, 0x2b);
            this.gbDefault.Name = "gbDefault";
            this.gbDefault.Size = new Size(0x1d9, 0x74);
            this.gbDefault.TabIndex = 3;
            this.gbDefault.TabStop = false;
            this.gbDefault.Text = "T\x00f9y chọn hiển thị";
            this.cheIsUperCompanyName.Location = new Point(0xd4, 0x19);
            this.cheIsUperCompanyName.Name = "cheIsUperCompanyName";
            this.cheIsUperCompanyName.Properties.Caption = "In hoa t\x00ean c\x00f4ng ty";
            this.cheIsUperCompanyName.Size = new Size(0x7a, 0x13);
            this.cheIsUperCompanyName.TabIndex = 0x13;
            this.cheIsShowLogo.Location = new Point(10, 0x19);
            this.cheIsShowLogo.Name = "cheIsShowLogo";
            this.cheIsShowLogo.Properties.Caption = "Hiển thị logo c\x00f4ng ty tr\x00ean b\x00e1o c\x00e1o";
            this.cheIsShowLogo.Size = new Size(0xc4, 0x13);
            this.cheIsShowLogo.TabIndex = 0x12;
            this.txtFontCompanyAddress.Location = new Point(0xd6, 0x52);
            this.txtFontCompanyAddress.Name = "txtFontCompanyAddress";
            this.txtFontCompanyAddress.Properties.ReadOnly = true;
            this.txtFontCompanyAddress.Size = new Size(0xcd, 20);
            this.txtFontCompanyAddress.TabIndex = 13;
            this.btFontCompanyName.Location = new Point(0x1a9, 0x35);
            this.btFontCompanyName.Name = "btFontCompanyName";
            this.btFontCompanyName.Size = new Size(0x24, 20);
            this.btFontCompanyName.TabIndex = 0x11;
            this.btFontCompanyName.Text = "...";
            this.btFontCompanyName.Click += new EventHandler(this.btFontCompanyName_Click);
            this.txtFontCompanyName.Location = new Point(0xd6, 0x35);
            this.txtFontCompanyName.Name = "txtFontCompanyName";
            this.txtFontCompanyName.Properties.ReadOnly = true;
            this.txtFontCompanyName.Size = new Size(0xcd, 20);
            this.txtFontCompanyName.TabIndex = 0x10;
            this.labelControl4.Location = new Point(13, 0x37);
            this.labelControl4.Name = "labelControl4";
            this.labelControl4.Size = new Size(0xb5, 13);
            this.labelControl4.TabIndex = 15;
            this.labelControl4.Text = "Định dạng ph\x00f4ng chữ cho t\x00ean c\x00f4ng ty";
            this.btFontCompanyAddress.Location = new Point(0x1a9, 0x52);
            this.btFontCompanyAddress.Name = "btFontCompanyAddress";
            this.btFontCompanyAddress.Size = new Size(0x24, 20);
            this.btFontCompanyAddress.TabIndex = 14;
            this.btFontCompanyAddress.Text = "...";
            this.btFontCompanyAddress.Click += new EventHandler(this.btFontCompanyAddress_Click);
            this.labelControl3.Location = new Point(13, 0x54);
            this.labelControl3.Name = "labelControl3";
            this.labelControl3.Size = new Size(0xc3, 13);
            this.labelControl3.TabIndex = 12;
            this.labelControl3.Text = "Định dạng ph\x00f4ng chữ cho địa chỉ c\x00f4ng ty";
            this.rdStyleDefault.AutoSize = true;
            this.rdStyleDefault.Location = new Point(0x10, 13);
            this.rdStyleDefault.Name = "rdStyleDefault";
            this.rdStyleDefault.Size = new Size(0x124, 0x11);
            this.rdStyleDefault.TabIndex = 4;
            this.rdStyleDefault.TabStop = true;
            this.rdStyleDefault.Text = "Hiển thị đầu trang theo dạng mặc định của chương tr\x00ecnh";
            this.rdStyleDefault.UseVisualStyleBackColor = true;
            this.rdStyleDefault.CheckedChanged += new EventHandler(this.rdStyleDefault_CheckedChanged);
            this.rdStyleImage.AutoSize = true;
            this.rdStyleImage.Location = new Point(0x10, 0xa9);
            this.rdStyleImage.Name = "rdStyleImage";
            this.rdStyleImage.Size = new Size(0xe1, 0x11);
            this.rdStyleImage.TabIndex = 5;
            this.rdStyleImage.TabStop = true;
            this.rdStyleImage.Text = "Hiển thị đầu trang theo h\x00ecnh ảnh b\x00ean dưới";
            this.rdStyleImage.UseVisualStyleBackColor = true;
            this.rdStyleImage.CheckedChanged += new EventHandler(this.rdStyleDefault_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
           // base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.rdStyleImage);
            base.Controls.Add(this.rdStyleDefault);
            base.Controls.Add(this.gbDefault);
            base.Controls.Add(this.gbImage);
            base.Name = "xucHeaderReportOption";
            base.Padding = new Padding(10);
            base.Size = new Size(0x1f3, 0x162);
            base.Load += new EventHandler(this.xucHeaderReportOption_Load);
            this.gbImage.ResumeLayout(false);
            this.gbImage.PerformLayout();
            this.calImageHeight.Properties.EndInit();
            this.calImageWidth.Properties.EndInit();
            this.ptImageHeader.Properties.EndInit();
            this.gbDefault.ResumeLayout(false);
            this.gbDefault.PerformLayout();
            this.cheIsUperCompanyName.Properties.EndInit();
            this.cheIsShowLogo.Properties.EndInit();
            this.txtFontCompanyAddress.Properties.EndInit();
            this.txtFontCompanyName.Properties.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LoadFormatOption()
        {
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(this.xmlFormatOptionPath);
                if (int.Parse(set.Tables[0].Rows[0]["Style"].ToString()) == 0)
                {
                    this.rdStyleDefault.Checked = true;
                }
                else
                {
                    this.rdStyleImage.Checked = true;
                }
                this.cheIsShowLogo.Checked = bool.Parse(set.Tables[0].Rows[0]["IsShowLogo"].ToString());
                this.cheIsUperCompanyName.Checked = bool.Parse(set.Tables[0].Rows[0]["IsUperCompanyName"].ToString());
                this.txtFontCompanyName.Text = set.Tables[0].Rows[0]["FontCompanyName"].ToString();
                this.txtFontCompanyAddress.Text = set.Tables[0].Rows[0]["FontCompanyAddress"].ToString();
                try
                {
                    byte[] buffer = Convert.FromBase64String(set.Tables[0].Rows[0]["ImageHeader"].ToString());
                    if (buffer.Length != 2)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        this.ptImageHeader.Image = Image.FromStream(stream);
                    }
                    if (buffer.Length == 2)
                    {
                        this.ptImageHeader.Image = null;
                    }
                }
                catch
                {
                    this.ptImageHeader.Properties.NullText = "Tập tin h\x00ecnh ảnh c\x00f3 thể bị x\x00f3a hoặc kh\x00f4ng tồn tại.\nNhấp chuột phải, chọn Load để mở tập tin mới!";
                    this.ptImageHeader.Image = null;
                }
                this.calImageWidth.EditValue = float.Parse(set.Tables[0].Rows[0]["ImageWidth"].ToString());
                this.calImageHeight.EditValue = float.Parse(set.Tables[0].Rows[0]["ImageHeight"].ToString());
            }
            catch
            {
                this.rdStyleDefault.Checked = true;
                this.cheIsShowLogo.Checked = true;
                this.cheIsUperCompanyName.Checked = true;
                this.txtFontCompanyName.Text = "Times New Roman, 11,25pt, style=Bold";
                this.txtFontCompanyAddress.Text = "Times New Roman, 11,25pt, style=Regular";
                this.ptImageHeader.Image = null;
                this.calImageWidth.EditValue = 500;
                this.calImageHeight.EditValue = 100;
            }
            try
            {
                FontConverter converter = new FontConverter();
                this.txtFontCompanyName.Font = converter.ConvertFromString(this.txtFontCompanyName.Text) as Font;
                this.txtFontCompanyAddress.Font = converter.ConvertFromString(this.txtFontCompanyAddress.Text) as Font;
            }
            catch
            {
            }
        }

        public void LoadOption()
        {
            this.LoadFormatOption();
        }

        private void ptImageHeader_ImageChanged(object sender, EventArgs e)
        {
            if (this.ptImageHeader.Image != null)
            {
                this.calImageWidth.EditValue = this.ptImageHeader.Image.Width;
                this.calImageHeight.EditValue = this.ptImageHeader.Image.Height;
                this.gbImage.Text = "Chọn h\x00ecnh ảnh (k\x00edch cỡ gốc: " + this.ptImageHeader.Image.Width.ToString() + " x " + this.ptImageHeader.Image.Height.ToString() + ")";
            }
        }

        private void rdStyleDefault_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdStyleDefault.Checked)
            {
                this.gbDefault.Enabled = true;
                this.gbImage.Enabled = false;
            }
            else
            {
                this.gbDefault.Enabled = false;
                this.gbImage.Enabled = true;
            }
        }

        public void Save()
        {
            this.SaveFormatOption();
        }

        public void SaveFormatOption()
        {
            byte[] buffer;
            ImageConverter converter = new ImageConverter();
            if (this.ptImageHeader.Image != null)
            {
                MemoryStream stream = new MemoryStream();
                this.ptImageHeader.Image.Save(stream, this.ptImageHeader.Image.RawFormat);
                buffer = stream.GetBuffer();
                stream.Close();
            }
            else
            {
                buffer = new byte[2];
            }
            DataSet set = new DataSet();
            using (DataTable table = new DataTable())
            {
                table.Columns.Add("Style", typeof(int));
                table.Columns.Add("IsShowLogo", typeof(bool));
                table.Columns.Add("IsUperCompanyName", typeof(bool));
                table.Columns.Add("FontCompanyName", typeof(string));
                table.Columns.Add("FontCompanyAddress", typeof(string));
                table.Columns.Add("ImageHeader", typeof(byte[]));
                table.Columns.Add("ImageWidth", typeof(float));
                table.Columns.Add("ImageHeight", typeof(float));
                table.Rows.Add(new object[0]);
                int num = 0;
                if (this.rdStyleDefault.Checked)
                {
                    num = 0;
                }
                else
                {
                    num = 1;
                }
                object[] objArray = new object[] { num, bool.Parse(this.cheIsShowLogo.Checked.ToString()), bool.Parse(this.cheIsUperCompanyName.Checked.ToString()), this.txtFontCompanyName.Text, this.txtFontCompanyAddress.Text, buffer, float.Parse(this.calImageWidth.EditValue.ToString()), float.Parse(this.calImageHeight.EditValue.ToString()) };
                table.Rows[0][0] = objArray[0];
                table.Rows[0][1] = objArray[1];
                table.Rows[0][2] = objArray[2];
                table.Rows[0][3] = objArray[3];
                table.Rows[0][4] = objArray[4];
                table.Rows[0][5] = objArray[5];
                table.Rows[0][6] = objArray[6];
                table.Rows[0][7] = objArray[7];
                set.Tables.Add(table);
            }
            try
            {
                set.WriteXml(this.xmlFormatOptionPath);
            }
            catch
            {
            }
        }

        private void xucHeaderReportOption_Load(object sender, EventArgs e)
        {
        }
    }
}

